<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Front_Controller extends MY_Controller {
	function __construct () {
		parent::__construct();
		
		$this->load->helper('ssl');
		
		// we are in the frontend
		define("_FRONTEND","TRUE");
		
		$this->load->driver('cache');
		
		$this->load->library('smarty');
		$this->load->library('user_agent');
		if($this->agent->is_mobile()){
			$this->smarty->initialize(true);
		}else {
			$this->smarty->initialize();
		}
			
		$modules = $this->module_model->get_module_folders();
		$this->load->model('Users/users_model');
		
		$this->module_definitions = new stdClass();
		
		foreach ($modules as $module) {
			MY_Loader::define_module($module . '/');
		}
		
		//$this->load->library('app_hooks');

		/////////////////////////////////////
		//LOGIN//////////////////////////////
		/////////////////////////////////////
		//$this->load->library('facebook');

		$user = $this->facebook->getUser();
        if ($user) {
            try {
            	$login = $this->session->userdata('logged_in');
            	if (isset($login['user_profile'])) {
                	$data['user_profile'] = $login['user_profile'];
				}
				else 
				{
					$data['user_profile'] = $this->facebook->api('/me');
					if (!$this->users_model->checkExistUser($data['user_profile']['id'])) {
						$this->users_model->insert_users($data['user_profile']);
					}
				}
            } catch (FacebookApiException $e) {
                $user = null;
            }
        }else {
            $this->facebook->destroySession();
        }

        if ($user) {

            $data['logout_url'] = site_url('logout'); // Logs off application
            // OR 
            // Logs off FB!
            // $data['logout_url'] = $this->facebook->getLogoutUrl();

        } else {
            $data['login_url'] = $this->facebook->getLoginUrl(array(
                'redirect_uri' => site_url(), 
                'scope' => array("email") // permissions here
            ));
        }

        $this->session->set_userdata('logged_in', $data);
        $this->smarty->assign('logged_in', $data);
	}
	
	protected function _get_cofig_paging($config = array()) {
	    $config['full_tag_open'] = '<div id="pagination" class="pagination">';
	    $config['full_tag_close'] = '</div>';
	    
	    $config['first_link'] = '&laquo;';
	    $config['first_tag_open'] = '<li class="page">';
	    $config['first_tag_close'] = '</li>';

	    $config['last_link'] = '&raquo;';
	    $config['last_tag_open'] = '<li class="page">';
	    $config['last_tag_close'] = '</li>';
	    
	    $config['next_link'] = '&rarr;';
	    $config['next_tag_open'] = '<li class="page">';
	    $config['next_tag_close'] = '</li>';
	     
	    $config['prev_link'] = '&larr;';
	    $config['prev_tag_open'] = '<li class="page">';
	    $config['prev_tag_close'] = '</li>';

	    $config['num_tag_open'] = '<li class="page">';
	    $config['num_tag_close'] = '</li>';
	    $config['cur_tag_open'] = '<li class="page active">';
	    $config['cur_tag_close'] = '</li>';

	    return $config;
	}
}